# 应用场景
# 1.我们去检查某个类是否有某种方法

class Company(object):
    def __init__(self, employee_list):
        self.employee = employee_list

    def __len__(self):
        return len(self.employee)


com = Company(['tom', 'jane'])
print(hasattr(com, '__len__'))
# 我们在某些情况下希望判定某个对象的类型
from collections.abc import Sized

print(isinstance(com, Sized))


# 我们与要强制某个子类必须实现某些方法
# 是想了一个web框架，继承
# 需要设计一个抽象基类，指定子类必须实现某些方法

# 如何去模拟一个抽象基类
class CaheBase():
    def get(self, key):
        raise NotImplementedError

    def set(self, key, value):
        raise NotImplementedError


class RedisCache(CaheBase):
    def set(self, key, value):
        pass


redis_cache = RedisCache()
redis_cache.set('key', 'value')

import abc


class CaheBase(metaclass=abc.ABCMeta):
    @abc.abstractmethod
    def get(self, key):
        pass

    @abc.abstractmethod
    def set(self, key, value):
        pass


class RedisCache(CaheBase):
    def get(self, key):
        pass

    def set(self, key, value):
        pass


redis_cache = RedisCache()
